const express = require("express");

const app = express();

// 框架内置，对外提供文件共享服务
// http://127.0.0.1:8081/1.html
// F:\code-211\express-demo\public\1.html === '/a/1.html'
const staticPublic = express.static("./public");
app.use("/a", staticPublic);

// F:\code-211\express-demo\assets\1.html === '/b/1.html'
const staticAssets = express.static("./assets");
app.use("/b", staticAssets);

// 解析报文
// name=jack&age=20&gender=boy -> {name:'jack',age:20,gender:'boy'}
app.use(express.urlencoded());
// JSON.parser
app.use(express.json());

// （中间件）补全代码实现功能...
const m1 = (req, res, next) => {
  console.log(1);
  next();
};
// http://127.0.0.1:8081/c
app.use("/c", m1);

// http://127.0.0.1:8081/user
app.get("/user", (req, res, next) => {
  // res.type("html");
  // res.send("get <h2>hello</h2> user");
  // res.type("json");
  // res.send(`{"name":"jack","age":20}`);
  // res.send({ name: "jack", age: 20 });
  // res.type("xml");
  // res.send(`
  // <user>
  //   <name>jack</name>
  //   <age>20</age>
  // </user>
  // `);
  // Content-Type: image/svg+xml
  // res.sendFile("F:/code-211/express-demo/1.dio.svg");
  // Content-Type: video/mp4
  // res.type('mp4')
  // res.sendFile("F:/video/7.express1.mp4");
  // res.sendFile("F:/code-211/express-demo/public/1.zip");

  res.status(504);
  res.send("hello");
});

// http://127.0.0.1:8081/user
app.post("/user", (req, res, next) => {
  res.send("post hello user");
});

// http://127.0.0.1:8081/user/1
app.put("/user/1", (req, res, next) => {
  res.send("put hello user");
});

// http://127.0.0.1:8081/user/1
app.delete("/user/1", (req, res, next) => {
  res.send("delete hi user");
});

// http://127.0.0.1:8081/user/1
app.patch("/user/1", (req, res, next) => {
  res.send("patch hello user");
});

// http://127.0.0.1:8081/users/100/group/200
app.get("/users/:id/group/:cid", (req, res) => {
  res.send(req.params);
});

// http://127.0.0.1:8081/users?name=jack&age=20
app.get("/users", (req, res) => {
  // res.send(req.query);
  // res.send(req.headers);
  res.send(req.header("name"));
});

// http://127.0.0.1:8081/users
app.post("/users", (req, res) => {
  res.send(req.body);
});

// 设置端口号
app.listen(8081, () => {
  // http://127.0.0.1:8081
  // 当服务器成功启动
  console.log("success");
});
